package com.wisemoresystem.seal.action.verify;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_bank_level;
import com.wisemoresystem.seal.model.t_bank_verify;
import com.wisemoresystem.seal.service.BankSealService;
import com.wisemoresystem.seal.service.BankService;
import com.wisemoresystem.seal.service.VerifyService;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 审核机构
 * 
 * @author Administrator
 *
 */
@Controller("ixVerify")
@Scope("prototype")
@RequestMapping("/ix/verify")
public class VerifyAction extends ProjBaseAction {

	@Autowired
	private VerifyService vService;
	@Autowired
	private BankService bService;
	@Autowired
	private BankSealService sbService;

	/**
	 * 机构审核列表
	 * 
	 * @param srh_name
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {

		List<Map<String, Object>> list = vService.selectVerify(page);

		ModelAndView mv = new ModelAndView("/jsp/verify/list");
		mv.addObject("list", list);
		return mv;
	}

	@RequestMapping("/result")
	public ModelAndView result(String verify_id) {

		t_bank_verify verify = vService.findObjById(verify_id,
				t_bank_verify.class);
		Map<String, Object> before = new HashMap<String, Object>();
		before = vService.turnToMap(before, verify.getBefore_v());
		Map<String, Object> after = new HashMap<String, Object>();
		after = vService.turnToMap(after, verify.getAfter_v());

		ModelAndView mv = new ModelAndView("/jsp/verify/get_result");
		mv.addObject("before", before);
		mv.addObject("after", after);
		mv.addObject("verify", verify);
		return mv;
	}

	/**
	 * 机构审核
	 * 
	 * @param verify
	 *            审核信息
	 * @param verify_id
	 *            审核 id
	 * @return
	 */
	@RequestMapping("/do_v")
	public ModelAndView do_v(t_bank_verify verify, String verify_id) {

		t_bank_verify ver = vService
				.findObjById(verify_id, t_bank_verify.class);
		ver.setFlag(verify.getFlag());
		ver.setNote(verify.getNote());
		ver.setUser_id(WebTools.getCurrentUserId());
		vService.updateObj(ver);

		if (ver.getFlag().equals("通过")) {
			Map<String, Object> before = new HashMap<String, Object>();
			before = vService.turnToMap(before, ver.getBefore_v());
			t_bank bank = bService.selectBankByCode(before.get("code") + "");
			if (ver.getFlag_note().equals("机构撤销审核")) {
				vService.delObj(bank);
			} else if (ver.getFlag_note().equals("机构修改审核")) {
				Map<String, Object> after = new HashMap<String, Object>();

				after = vService.turnToMap(after, ver.getAfter_v());
				bank.setCode(after.get("code") + "");
				bank.setName(after.get("name") + "");
				bank.setCity_code(after.get("city_code") + "");
				bank.setLevel_id(after.get("level_id") + "");
				bank.setArea_id(after.get("area_id") + "");
				bank.setType(after.get("type") + "");
				bank.setFlag("启用");
				bank.setNote(after.get("note") + "");
				bank.setSuper_id(after.get("super_id") + "");
				vService.updateObj(bank);

				t_bank_level lev = vService.findObjById(before.get("level_id")
						+ "", t_bank_level.class);
				t_bank_level lv = vService.findObjById(after.get("level_id")
						+ "", t_bank_level.class);
				bService.updateLowerBank(bank.get_id(),
						lev.getSort() - lv.getSort());
			} else {
				Map<String, Object> after = new HashMap<String, Object>();
				after = vService.turnToMap(after, ver.getAfter_v());
				bank = new t_bank();
				bank.setCode(after.get("code") + "");
				bank.setName(after.get("name") + "");
				bank.setCity_code(after.get("city_code") + "");
				bank.setLevel_id(after.get("level_id") + "");
				bank.setArea_id(after.get("area_id") + "");
				bank.setType(after.get("type") + "");
				bank.setFlag("启用");
				bank.setNote(after.get("note") + "");
				bank.setUser_id(after.get("bank_user_id") + "");
				bank.setSuper_id(after.get("super_id") + "");
				vService.saveObj(bank);
			}

			sbService.checkSealBankCode(bank.get_id());

			logT.logH("通过机构审核", t_bank.class, bank.get_id());
		} else {
			Map<String, Object> before = new HashMap<String, Object>();
			before = vService.turnToMap(before, ver.getBefore_v());
			t_bank bank = bService.selectBankByCode(before.get("code") + "");
			if (ChkTools.isNotNull(bank)) {
				bank.setFlag("启用");
				vService.updateObj(bank);
				logT.logH("拒绝机构审核", t_bank.class, bank.get_id());
			}
		}

		super.setTipMsg("操作成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 机构批量审核
	 * 
	 * @param check_ids
	 *            检查信息的所有id
	 * @param type
	 *            类型
	 * @return
	 */
	@RequestMapping("/result_form")
	public ModelAndView result_form(String check_ids, String type) {

		String msg = vService.muli_choose(check_ids, type);
		if (ChkTools.isNotNull(msg)) {
			super.setTipMsg("操作成功！", Tip.Type.success);
		} else {
			super.setTipMsg("操作不成功！", Tip.Type.success);
		}
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}
}
